<?php
//永不超时 --cli  不能使用nginx  apache
//
set_time_limit(0);
include __DIR__.'/function.php';
require __DIR__.'/vendor/autoload.php';
use QL\QueryList;
$db = new PDO('mysql:host=localhost;dbname=bd_laravel;charset=utf8mb4','root','root');
$range = range(1,7);
$rules = [
    'pic' => ['.lj-lazy', 'data-original','',function($item){
        //这里需要对图片进行处理
        //得到图片的后缀名
        $ext = pathinfo($item,PATHINFO_EXTENSION);
        //生成文件名
        $filename = md5($item).'_'.time().'.'.$ext;
        //生成本地路径
        $filepath = dirname(__DIR__).'/public/uploads/article/'.$filename;
        file_put_contents($filepath,http_request($item));
        return  'uploads/article/'.$filename;
    }],
    'title' => ['.text .LOGCLICK','text'],
    'desn' => ['.text .summary', 'text'],
    'url' => ['.text > a','href']
];
//切片选择器
$change = '.m-col .item';
foreach($range as $page){
    $url = 'https://news.ke.com/bj/baike/033/pg'.$page.'/';
    $detaillist = QueryList::get($url)->rules($rules)->range($change)->query()->getData();
    //入库
    foreach($detaillist as $val){
        //判断标题是否存在，存在则不进行入库
        //添加的sql预处理
        $sql = "insert into bd_articles (title,desn,pic,url,body) values(?,?,?,?,'')";
        $stmt = $db->prepare($sql);
        //入库操作
        $stmt->execute([$val['title'],$val['desn'],$val['pic'],$val['url']]);
    }
}
?>